package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallCartGoodsPropertyMapper;
import org.linlinjava.litemall.db.domain.LitemallCartGoodsProperty;
import org.linlinjava.litemall.db.domain.LitemallCartGoodsPropertyExample;
import org.springframework.stereotype.Service;

/**
 * @author ：lilu
 */
@Service
public class LitemallCartGoodsPropertyService extends MPJBaseServiceImpl<LitemallCartGoodsPropertyMapper,LitemallCartGoodsProperty> {
	
    @Resource
    private LitemallCartGoodsPropertyMapper litemallCartGoodsPropertyMapper;

    public LitemallCartGoodsProperty findById(Integer id) {
        return litemallCartGoodsPropertyMapper.selectByPrimaryKey(id);
    }

    public int add(LitemallCartGoodsProperty record){
        record.setAddTime(LocalDateTime.now());
        return litemallCartGoodsPropertyMapper.insertSelective(record);
    }

    public int update(LitemallCartGoodsProperty record){
        return litemallCartGoodsPropertyMapper.updateByPrimaryKeySelective(record);
    }


    public int deleteById(Integer id) {
        return litemallCartGoodsPropertyMapper.deleteByPrimaryKey(id);
    }
    

    public List<LitemallCartGoodsProperty> queryByCartId(Integer cartId){
    	LitemallCartGoodsPropertyExample example = new LitemallCartGoodsPropertyExample();
    	example.or().andCartIdEqualTo(cartId).andDeletedEqualTo(false);
    	
    	return litemallCartGoodsPropertyMapper.selectByExample(example);
    }
    
    public int deleteByCartIds(List<Integer> cartIds){
    	LitemallCartGoodsPropertyExample example = new LitemallCartGoodsPropertyExample();
    	example.or().andCartIdIn(cartIds).andDeletedEqualTo(false);
    	
    	return litemallCartGoodsPropertyMapper.logicalDeleteByExample(example);
    }
    
    public Long countByCartId(Integer cartId){
    	LitemallCartGoodsPropertyExample example = new LitemallCartGoodsPropertyExample();
    	example.or().andCartIdEqualTo(cartId).andDeletedEqualTo(false);
    	
    	return litemallCartGoodsPropertyMapper.countByExample(example);
    }
    
    public LitemallCartGoodsProperty findByCartIdAndGoodsPropertyIdAndValue(Integer cartId, Integer goodsPropertyId, String value) {
    	LitemallCartGoodsPropertyExample example = new LitemallCartGoodsPropertyExample();
    	example.or().andCartIdEqualTo(cartId).andGoodsPropertyIdEqualTo(goodsPropertyId).andValueEqualTo(value).andDeletedEqualTo(false);
    
    	return litemallCartGoodsPropertyMapper.selectOneByExample(example);
    }
    
    public boolean checkPropertyIdExist(Integer propertyId) {
    	LitemallCartGoodsPropertyExample example = new LitemallCartGoodsPropertyExample();
    	example.or().andGoodsPropertyIdEqualTo(propertyId).andDeletedEqualTo(false);
    	return litemallCartGoodsPropertyMapper.countByExample(example) != 0;
    }
    
}
